import { GetActDetail, GetTeacherActiveDetail } from "../../../../../utils/data/apis";
import { event } from "../../../../../utils/interface";
import msg from "../../../../../utils/model/msg";

// pages/individual/active_detail/components/teacher/index.ts
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    user: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    activeDetail: {}, // 导师正在活动
    studentInfo: [{}], // 学生信息
    studentIndex: 0, // 当前查看的学生信息
    detailType: '基本信息', // 信息类型
    locationEaseOut: false, // 位置信息淡出动画
    showLocation: false, // 是否显示位置信息框
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * @function 获取页面数据
     */
    getData: async function() {
      const user: any = this.data.user;
      if(!user) {
        this.setData({
          activeDetail: '',
        })
        return;
      }
      msg.Loading();
      // 获取正在活动数据
      GetTeacherActiveDetail({
        id: user.id
      })
      .then((res: any) => {
        console.log(res.data);
        const courseid: string = res.data.tea_doing[0].courseid; // 获取课程id
        this.getActDetail(courseid); // 获取课程详细信息
        this.setData({
          studentInfo: res.data.data,
        })
        msg.HideLoading();
      })
      .catch(() => {
        this.setData({
          activeDetail: '',
        })
        msg.HideLoading();
      })
    },
    /**
     * @function 获取活动详情
     * @param courseid 课程id
     */
    getActDetail: function(courseid: string) {
      GetActDetail({
        id: courseid,
        modelName: 'ClubNews',
      })
      .then((res: any) => {
        const {title, sign_date_start, sign_date_end} = res.data.data[0];
        const activeDetail: {[key: string]: any} = this.data.activeDetail;
        activeDetail.title = title, activeDetail.sign_date_start = sign_date_start, activeDetail.sign_date_end = sign_date_end;
        this.setData({
          activeDetail: activeDetail
        })
      })
    },
    /**
     * @function 信息类型改变
     * @param e 
     */
    filterChange: function(e: event) {
      this.setData({
        detailType: e.detail.type,
      })
    },
    /**
     * @function 选择器->选择查看的学生
     */
    pickStudent: function(e: event) {
      const index = e.detail.value;
      this.setData({
        studentIndex: index,
      })
    },
    /**
     * @function 展示位置信息框
     */
    showLocation: function() {
      let status: boolean = !this.data.showLocation;
      if(!status) {
        // 折叠
        this.setData({
          locationEaseOut: true, // 淡出动画
        })
      }
      else {
        this.setData({
          showLocation: status,
        })
        return;
      }
      const that = this;
      setTimeout(() => {
        that.setData({
          showLocation: status,
          locationEaseOut: false,
        })
      }, 250)
    },
  },
  lifetimes: {
    attached: function() {
      this.getData();
    }
  }
})
